import React from 'react'
// ===================================================================== Declare
import Model44014 from '../js/search/14'
import Model44013 from '../js/search/13'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const SearchForm 	= $Lazy.state(()=>import('#cpt/form/search-form'))
const $time 		= $Lazy.file(()=>import('@com/utils/time'))
// ===================================================================== 
class Index extends React.Component{
	state = { 
		forms : []
	}
	query = { }
	model = { }
	// 根所不同 tabs 状态改变搜索表单
	getSearch = (forms,status) => {
		status = +status 
		if(this.isBingLi){
			if( status === 45003){ // 提交时间
				$fn.hideRows(forms, 'submit-time', false)
				$fn.hideRows(forms, ['audit-time', 'jz-time'], true)
			} else if(status === 45004){ // 审核时间
				$fn.hideRows(forms, 'audit-time', false)
				$fn.hideRows(forms, ['submit-time', 'jz-time'], true)
			}else if(status === 45006){
				$fn.setLabel(forms, 'jz-time', '迟发时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'submit-time' ], true)
			}else if(status === 45007){
				$fn.setLabel(forms, 'jz-time', '退单时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'submit-time' ], true)
			}else if(status === 45008){
				$fn.setLabel(forms, 'jz-time', '终止时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'submit-time' ], true)
			}else{
				$fn.setLabel(forms, 'jz-time', '录入时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'submit-time' ], true)
			}
		}else{
			if(status === 45002){ // 提交时间
				$fn.hideRows(forms, 'submit-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'approve-time', 'jz-time' ], true)
			}else if(status  === 45003){ // 审核时间
				$fn.hideRows(forms, 'audit-time', false)
				$fn.hideRows(forms, [ 'submit-time', 'approve-time', 'jz-time' ], true)
			}else if(status === 45004){ // 批准时间
				$fn.hideRows(forms, 'approve-time', false)
				$fn.hideRows(forms, [ 'submit-time', 'audit-time', 'jz-time' ], true)
			}else if(status === 45006){
				$fn.setLabel(forms, 'jz-time', '迟发时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'approve-time', 'submit-time' ], true)
			}else if(status === 45007){
				$fn.setLabel(forms, 'jz-time', '退单时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'approve-time', 'submit-time' ], true)
			}else if(status === 45008){
				$fn.setLabel(forms, 'jz-time', '终止时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'approve-time', 'submit-time' ], true)
			}else{
				$fn.setLabel(forms, 'jz-time', '录入时间')
				$fn.hideRows(forms, 'jz-time', false)
				$fn.hideRows(forms, [ 'audit-time', 'approve-time', 'submit-time' ], true)
			}
		}
	}
	
	componentDidMount(){
		let forms = []
		const { result_model, BingLiMode, query } = this.props.top
		this.result_model = result_model
		this.status = query.status
		this.query = query
		this.isBingLi = BingLiMode.includes(result_model)
		if(this.isBingLi){
			forms = Model44013.forms
			Model44013.getSelect.call(this, forms)
		}else{
			forms = Model44014.forms
			Model44014.getSelect.call(this, forms)
		}
		// 显示个性搜索
		this.getSearch(forms ,query.status)
		// 显示组织病理个性搜索
		$fn.hideRows(forms, ['draw_materials_doctor_id','record_id'], (result_model !== 44021 && result_model !== 44033))
		// // 液基细胞病理岗
		// $fn.hideRows(forms, ['result_proposal','suggest'], result_model !== 44008)
		// $fn.hideRows(forms, ['naked_eye','pathology','pathological_diagnosis'], result_model === 44008)
		// // 甲状腺岗
		// $fn.hideRows(forms, ['result_proposal','suggest'], result_model !== 44032)
		// $fn.hideRows(forms, ['naked_eye','pathology','pathological_diagnosis'], result_model === 44032)
		// // this.showSearch(forms, this.status)
		
		// 液基细胞病理岗 and 甲状腺岗 2022-1-17 12:54:45 ?F10: PM? --伍智睿
		if(result_model === 44008 || result_model === 44032){
			$fn.hideRows(forms, ['naked_eye','pathology','pathological_diagnosis'], true)
			$fn.hideRows(forms, ['result_proposal','suggest'], false)
		}else{
			$fn.hideRows(forms, ['naked_eye','pathology','pathological_diagnosis'], false)
			$fn.hideRows(forms, ['result_proposal','suggest'], true)
		}
		this.setState({ forms })
	}
	componentWillUnmount(){
		this.setState = () => false
	}
	// 显示个性搜索
	showSearch = (forms, status) => {
		// if(this.result_model === 44008){
		// 	$fn.hideRows(forms, ['visit_doctor_id','return_visit_doctor_id'], ([45003, 45004].includes(status)))
		// }
	}
	
	change = async status => {
		const { forms } = this.state
		this.getSearch(forms, status)
		this.status = status
		/* 2021-09-27 j+2 所有时间不默认 */
		await this.setTime()		
		this.onSubmit()
		this.setState({ forms })
	}
	
	// 获取搜索参数
	model = {}
	getModel = param => {
		this.model = { ...this.model, status: this.status, ...param, }
		return { ...this.query, ...this.model  }
	}
	onSubmit = param => {
		if(this.props.top?.way && !this.isBingLi){		// 分栏模式排序区分左右表格 2021-9-13 --马婷 
			if(param?.isTable !== undefined){
				if(param?.isTable){
					this.props.tableRef()?.change(this.getModel({ ...param, current:1 }))
				}else{
					this.props.tableRef2()?.change(this.getModel({ ...param, current:1 }))
				}
			}else{
				this.props.tableRef()?.change(this.getModel({ ...param, current:1 }))
				this.props.tableRef2()?.FetchFirst(this.getModel({ ...param, current:1 }))
				this.props.tableRef2().setState({data: [],selectedKeys:[],selectedRows:[]})
			}
		}else{
			this.props.tableRef()?.change(this.getModel({ ...param, current:1 }))
		}
	}
	onRefresh = param => {
		if(this.props.top.way && param?.rowsType !== undefined && !this.isBingLi){		// 分栏模式刷新区分左右表格 2021-9-13 --马婷
			if(+param.rowsType === 1){
				this.props.tableRef().change(this.getModel({ ...param } ))
				this.props.tableRef2().setState({data: [],selectedKeys:[],selectedRows:[]})
				// this.props.tableRef2()?.change(this.getModel({ ...param } ))
			}else{
				this.props.tableRef2()?.change(this.getModel({ ...param } )).then(data => !data.length && this.props.tableRef().change(this.getModel({ ...param } )))
			}
		}else{
			this.props.tableRef().change(this.getModel({ ...param } ))
		}
	}
	// 分页
	pageChange = ({current, pageSize}) => {
		this.model = {...this.model, current, pageSize }
		this.props.tableRef().change(this.getModel(this.model))
	}
	// 设置时间
	setTime = async () => {
		// 设置默认时间一个月 2021-9-17 9:57:04 ?F10: AM? --伍智睿
		const initTime = async ({name, names = ['start_date', 'end_date'], time = '30'}) => {
			await $time.then(f=>{
				const arr = f.duration[time]()
				const param = { [names[0]]: arr[0], [names[1]]: arr[1] }
				this.resetModel = { ...this.resetModel, [name]: arr }
				this.model = {...this.model, ...param}
				this.formRef.setValue({[name]: arr})
			})
		}
		const clearTime = ({name, names = ['start_date', 'end_date']}) => {
			this.model[names[0]] = ''
			this.model[names[1]] = ''
			this.formRef.setValue({ [name]: ''})
			this.resetModel = {}
		}
		if(this.isBingLi){
			if( this.status === 45001 || this.status === 45006 || this.status === 45007 || this.status === 45008 ) { // 录入、迟发、退单、终止时间
				if (this.query?.type) {
					initTime({name: 'jz-time', time: '60'})
				} else {
					initTime({name: 'jz-time'})
				}
			} else {
				clearTime({name: 'jz-time'})
			}
			if( this.status === 45003 ){ //  提交时间
				initTime({name: 'submit-time', names: ['ts_user_start_at', 'ts_user_end_at']})
			} else {
				clearTime({name: 'submit-time', names: ['ts_user_start_at', 'ts_user_end_at']})
			}
			if(this.status === 45004){ // 设置病理已审核的默认审核时间
				initTime({name: 'audit-time', names: ['ts_audit_start_at', 'ts_audit_end_at'], time: '2-9-12'})
			}else{
				clearTime({name: 'audit-time', names: ['ts_audit_start_at', 'ts_audit_end_at']})
			}
		}else{
			if( this.status === 45001 || this.status === 45006 || this.status === 45007 || this.status === 45008 ) { // 录入、迟发、退单、终止时间
				if (this.query?.type) {
					initTime({name: 'jz-time', time: '60'})
				} else {
					initTime({name: 'jz-time'})
				}
			} else {
				clearTime({name: 'jz-time'})
			}
			if( this.status === 45002 ){ //  录入时间
				initTime({name: 'submit-time', names: ['ts_user_start_at', 'ts_user_end_at']})
			} else {
				clearTime({name: 'submit-time', names: ['ts_user_start_at', 'ts_user_end_at']})
			}
			if(this.status === 45003){ //  提交时间
				initTime({name: 'audit-time', names: ['ts_audit_start_at', 'ts_audit_end_at']})
			} else {
				clearTime({name: 'audit-time', names: ['ts_audit_start_at', 'ts_audit_end_at']})
			}
			if(this.status === 45004){ //  审核时间
				initTime({name: 'approve-time', names: ['ts_approve_start_at', 'ts_approve_end_at'], time: '2-9-12'})
			} else {
				clearTime({name: 'approve-time', names: ['ts_approve_start_at', 'ts_approve_end_at']})
			}
		}
	}
	
	// 排序
	onSort = v => {
		const { value, isTable } = v		 // 排序区分左右表格 2021-9-13 --马婷 
		if(value){
			this.model = {...this.model, ...value, isTable}
		}else{
			delete this.model.sort
			delete this.model.sort_type
		}
		this.onSubmit(this.model)
	}
	clear = () => this.props.tableRef().clear()
	
	setValue = value => this.formRef.setValue(value)
	
	render(){
		const { forms } = this.state
		return (
			<>
				<SearchForm
					className 	= 'pt5'
					data	 	= { forms }
					onChange	= { ({ model }) => { this.model = {...this.model, ...model} } }
					onSubmit	= { ()=>{
						this.model = {...this.model, current:1 }
						this.clear()
						this.onSubmit()
					} }
					onReset		= { ()=> {
						this.model = { pageSize:window.$config.pageSize, current:1 }
						if($fn.hasObject(this.resetModel)){
							// 增加 提交 和 审核 2021-9-18 2:04:12 ?F10: PM? --伍智睿
							const auditArr = this.resetModel['audit-time'] ?? []
							const jzArr = this.resetModel['jz-time'] ?? []
							const submitArr = this.resetModel['submit-time'] ?? []
							const approveArr = this.resetModel['approve-time'] ?? []
							this.formRef?.setValue?.(this.resetModel)
							this.model = { 
								ts_audit_start_at: auditArr[0] ?? '', 
								ts_audit_end_at: auditArr[1] ?? '',
								start_date: jzArr[0] ?? '',
								end_date: jzArr[1] ?? '',
								ts_user_start_at: submitArr[0] ?? '',
								ts_user_end_at: submitArr[1] ?? '',
								ts_approve_start_at: approveArr[0] ?? '',
								ts_approve_end_at: approveArr[1] ?? ''
							}
						}
						this.props.tableRef().change(this.getModel())
						this.props.top.way && !this.isBingLi &&  this.props.tableRef2()?.change(this.getModel())
						this.props.tableRef().reset()
						this.props.top.way && !this.isBingLi && this.props.tableRef2()?.reset()
					} }
					onForm 		= { async ref => {
						this.formRef = ref
						/* 2021-10-22 j+2 所有时间默认 */
						await this.setTime()	
						this.props.tableRef().FetchFirst(this.getModel({ current:1 }))
						this.props.top.way && !this.isBingLi && this.props.tableRef2()?.FetchFirst(this.getModel({ current:1 }))
					}}
				/>
			</>
		)
	}
}

export default Index